#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
数据库查询测试文件
使用 db/mysql.py 中的配置进行数据库查询
"""

import sys
import os

# 添加项目根目录到路径
project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(project_root)

from db.mysql import MySQLDB
import logging

logger = logging.getLogger(__name__)


class PromptKnowledgeBaseDao:
    """数据库查询测试类"""
    
    def __init__(self):
        self.db = MySQLDB()
    
    def execute_sql(self, sql, params=None):
        """执行SQL查询的通用方法"""
        try:
            connection = self.db.get_connection()
            if not connection:
                logger.error("无法获取数据库连接")
                return []
            
            cursor = connection.cursor(dictionary=True)
            
            if params:
                cursor.execute(sql, params)
            else:
                cursor.execute(sql)
            
            results = cursor.fetchall()
            cursor.close()
            
            logger.info(f"SQL执行成功，返回 {len(results)} 条记录")
            return results
            
        except Exception as e:
            logger.error(f"SQL执行失败: {e}")
            return []
    
    def query_base_usr(self):
        sql = """
        SELECT 
            t.id,
             t.cue_word_name,
             t.cue_word_content,
             t.agent_name
         
        FROM hospital_dip_agent t
        
       
        """
        
        logger.info("执行查询: 获取所有患者信息")
        return self.execute_sql(sql)
    

